<script lang="ts" setup>
import Utils from "../../../req/Utils.js";
import { showToast} from "vant";

import { useRouter } from 'vue-router';
const router = useRouter();

const props = defineProps({
  obj: {
      type: Object
    }
});

const addCart = (product: any) => {
  Utils.addCart(product);
  showToast("添加商品成功");
};
const getImg = (str: String) => {
  return Utils.getImg(str, 0);
};
const toDetail = (id:any) => {
  router.push({path: "/productDetail", query: {productId: id}});
};
</script>
<template>
  <section class="productItemUtil" v-if="props.obj" @click="toDetail(props.obj.productId)">
    <div class="img-icon" :style="{background: `#ccc url(${props.obj.productIcon}) no-repeat center`}"></div>
    <div class="desc">{{props.obj.productName}}</div>
    <div class="price-wrap">
      <div class="money">
        <span>￥</span>{{props.obj.productPrice}}
      </div>
      <div class="cart" @click.stop="addCart(props.obj)"></div>
    </div>
  </section>
</template>

<style lang="less" scoped>
.productItemUtil {
  width: 50%;
  height: 2.48rem;
  position: relative;
  float: left;
  padding: .15rem .15rem 0;
  box-sizing: border-box;
  &::before {
    height: 0;
    width: 100%;
    content: "";
    position: absolute;
    left: 0;
    bottom: 0;
    border-bottom: .01rem solid #d2cbcb;
  }
  &:nth-of-type(2n) {
    float: right;
    &::after {
      width: 0;
      height: 100%;
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      border-left: .01rem solid #d2cbcb;
    }
  }
  .img-icon {
    width: 1.57rem;
    height: 1.57rem;
    background-size: cover !important;
    margin-bottom: .1rem;
  }
  .desc {
    line-height: 0.17rem;
    // padding-right: 0.1rem;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    color: #232326;
    font-size: 0.12rem;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    margin-bottom: 0.06rem;
  }
  .price-wrap {
    display: flex;
    .money {
      color: #e93b3d;
      font-size: .16rem;
      flex: 1;
      span {
        font-size: .12rem;
      }
    }
    .cart {
      width: .3rem;
      height: .24rem;
      background: url("../../../assets/images/cart-icon-5.png") no-repeat center;
      background-size: 50%;
    }
  }
}
</style>
